<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="水电师傅" prop="workerId">
        <el-input
          v-model="queryParams.workerId"
          placeholder="请输入水电师傅"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户" prop="consumerId">
        <el-input
          v-model="queryParams.consumerId"
          placeholder="请输入客户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总价" prop="totalMoney">
        <el-input
          v-model="queryParams.totalMoney"
          placeholder="请输入总价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="orderStatus">
        <el-select  v-model="queryParams.orderStatus" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            @keyup.enter.native="handleQuery">
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            @keyup.enter.native="handleQuery">
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="付款状态" prop="orderSale">
        <el-select v-model="queryParams.orderSale" placeholder="请选择类型" clearable size="small">
          <el-option
            v-for="item in options3"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            @keyup.enter.native="handleQuery">
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="欠款" prop="orderDebt">
        <el-input
          v-model="queryParams.orderDebt"
          placeholder="请输入结算单欠款"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算单号" prop="closeOrder">
        <el-input
          v-model="queryParams.closeOrder"
          placeholder="请输入结算单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开单时间" prop="orderDate">
        <el-input
          v-model="queryParams.orderDate"
          placeholder="请输入开单时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除" prop="isDel">
        <el-input
          v-model="queryParams.isDel"
          placeholder="请输入删除"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sale:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sale:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sale:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sale:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderId" />
      <el-table-column label="水电师傅" align="center" prop="workerId" />
      <el-table-column label="客户" align="center" prop="consumerId" />
      <el-table-column label="总价" align="center" prop="totalMoney" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.orderStatus==0">未打印</span>
          <span v-if="scope.row.orderStatus==1">已打印</span>
          <span v-if="scope.row.orderStatus==2">已结算</span>
          <span v-if="scope.row.orderStatus==3">未结清</span>
          <span v-if="scope.row.orderStatus==4">已结清</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.orderType==1">销售单</span>
          <span v-if="scope.row.orderType==2">结算单</span>
        </template>
      </el-table-column>
      <el-table-column label="付款状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSale==0">未付清</span>
          <span v-if="scope.row.orderSale==1">已付清</span>
        </template>
      </el-table-column>
      <el-table-column label="欠款" align="center" prop="orderDebt" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="结算单号" align="center" prop="closeOrder" />
      <el-table-column label="开单时间" align="center" prop="orderDate" />
      <el-table-column label="删除" align="center" prop="isDel" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sale:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sale:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改销售订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="水电师傅" prop="workerId">
          <el-input v-model="form.workerId" placeholder="请输入水电师傅" />
        </el-form-item>
        <el-form-item label="客户" prop="consumerId">
          <el-input v-model="form.consumerId" placeholder="请输入客户" />
        </el-form-item>
        <el-form-item label="总价" prop="totalMoney">
          <el-input v-model="form.totalMoney" placeholder="请输入总价" />
        </el-form-item>
        <el-form-item label="状态">
            <el-select v-model="form.orderStatus"  clearable>
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                size="small">
                <span style="float: left">{{ item.label }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="orderType">
          <el-select v-model="form.orderType"  clearable>
            <el-option
              v-for="item in options2"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="small">
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结算单付款状态" prop="orderSale">
          <el-select v-model="form.orderSale"  clearable>
            <el-option
              v-for="item in options3"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="small">
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结算单欠款" prop="orderDebt">
          <el-input v-model="form.orderDebt" placeholder="欠款" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="结算单号" prop="closeOrder">
          <el-input v-model="form.closeOrder" placeholder="请输入结算单号" />
        </el-form-item>
        <el-form-item label="开单时间" prop="orderDate">
          <el-input v-model="form.orderDate" placeholder="请输入开单时间" />
        </el-form-item>
        <el-form-item label="删除" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/sale/order";

export default {
  name: "Order",
  components: {
  },
  data() {
    return {
      options: [{
        value: 0,
        label: '未打印'
      },{
        value: 1,
        label: '已打印'
      },{
        value: 2,
        label: '已结算'
      },{
        value: 3,
        label: '未结清'
      },{
        value: 4,
        label: '已结清'
      }],
      options2: [{
        value: 1,
        label: '销售单'
      },{
        value: 2,
        label: '结算单'
      }],
      options3: [{
        value: 0,
        label: '未付清'
      },{
        value: 1,
        label: '已付清'
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workerId: null,
        consumerId: null,
        totalMoney: null,
        orderStatus: null,
        orderType: null,
        orderSale: null,
        orderDebt: null,
        closeOrder: null,
        orderDate: null,
        isDel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workerId: [
          { required: true, message: "水电师傅不能为空", trigger: "blur" }
        ],
        consumerId: [
          { required: true, message: "客户不能为空", trigger: "blur" }
        ],
        totalMoney: [
          { required: true, message: "总价不能为空", trigger: "blur" }
        ],
        orderStatus: [
          { required: true, message: "状态(0-未打印；1-已打印；2-已结算；3-未结清；4-已结清)不能为空", trigger: "blur" }
        ],
        orderType: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        orderSale: [
          { required: true, message: "结算单付款状态(0-未付清；1-已付清)不能为空", trigger: "blur" }
        ],
        orderDebt: [
          { required: true, message: "结算单欠款不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        closeOrder: [
          { required: true, message: "结算单号不能为空", trigger: "blur" }
        ],
        isDel: [
          { required: true, message: "删除不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销售订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        workerId: null,
        consumerId: null,
        totalMoney: null,
        orderStatus: "0",
        orderType: null,
        orderSale: null,
        orderDebt: null,
        remark: null,
        closeOrder: null,
        orderDate: null,
        isDel: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改销售订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$confirm('是否确认删除销售订单编号为"' + orderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(orderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有销售订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
